package elitech.vietnam.weatherfashion;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import elitech.vietnam.weatherfashion.backgroundtask.RegisterTask;

public class RegisterActivity extends Activity implements OnClickListener {

	private EditText textbox_username, textbox_password, textbox_password2;
	private Button button_register;
	private TextView text_error;

	// Sprivate Spinner spinerQuocGia;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);

		textbox_username = (EditText) findViewById(R.id.register_textbox_username);
		textbox_password = (EditText) findViewById(R.id.register_textbox_password);
		textbox_password2 = (EditText) findViewById(R.id.register_textbox_password2);
		button_register = (Button) findViewById(R.id.register_button_register);
		text_error = (TextView) findViewById(R.id.register_text_error);
		button_register.setOnClickListener(this);

		/*************************************/

		// hide keyboard
		try {
			getWindow().setSoftInputMode(
					WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		} catch (Exception e) {
		}
	}

	@Override
	public void onClick(View v) {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(textbox_username.getWindowToken(), 0);
		imm.hideSoftInputFromWindow(textbox_password.getWindowToken(), 0);
		imm.hideSoftInputFromWindow(textbox_password2.getWindowToken(), 0);
		switch (v.getId()) {
		case R.id.register_button_register:
			text_error.setText("");
			String msg = validate();
			if (msg != null)
				setError(msg);
			else {
				// String[] arrCountryCode = { "KR", "VN", "US" };

				new RegisterTask(RegisterActivity.this).execute(
						textbox_username.getText().toString(), textbox_password
								.getText().toString());
			}
			break;

		default:
			break;
		}
	}

	public void setError(String error) {
		text_error.setText(error);
	}

	private String validate() {
		if (textbox_username.getText().toString().equals(""))
			return getString(R.string.please_enter_username);
		if (textbox_password.getText().toString().equals(""))
			return getString(R.string.please_enter_password);
		if (textbox_password2.getText().toString().equals(""))
			return getString(R.string.please_confirm_password);
		if (!textbox_password.getText().toString()
				.equals(textbox_password2.getText().toString()))
			return getString(R.string.the_password_confirmed_did_not_match_);
		return null;
	}

	@Override
	protected void onStop() {
		/*************************************/
		// hide keyboard
		try {
			getWindow().setSoftInputMode(
					WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		} catch (Exception e) {
		}
		super.onDestroy();
	}

}
